package com.yager.util;

import com.yager.model.Comment;
import com.yager.model.House;
import com.yager.model.Order;
import com.yager.model.Page;

import java.util.ArrayList;
import java.util.List;

public class DividePageUtil {

    public static Page divideHousePage(List<House> list,List<String> imageList,int pageNo,int pageSize){

        List<House> result=list.subList((pageNo-1)*pageSize,pageNo*pageSize);
        List<String> imageResult=imageList.subList((pageNo-1)*pageSize,pageNo*pageSize);

        Page page=new Page(pageNo,pageSize,list.size());

        int startpage,endpage;

        System.out.println("pageNo="+pageNo+"   totalnum"+page.getTotalPageNum());

        if(pageNo-2>=1) startpage=pageNo-2;
        else startpage=1;

//        pageNo-page.getTotalPageNum()+2;

        if(pageNo+2>page.getTotalPageNum()){
            System.out.println(pageNo+"   "+page.getTotalPageNum());
            if(pageNo==page.getTotalPageNum()){
                if(pageNo-4<1) startpage=1;
                else  startpage=pageNo-4;
            }
            else {
                System.out.println("按理说是5 实际上是"+pageNo);
                if(pageNo-3<1) startpage=1;
                else startpage=pageNo-3;
            }
        }



        List<Integer> numList=new ArrayList<>();
        for(int i=0;i<5&&startpage<=page.getTotalPageNum();i++){
            numList.add(startpage);
            startpage++;
        }
        System.out.println("numList内容为"+numList);
        page.setPageNumList(numList);

        page.setHouseList(result);
        page.setImageList(imageResult);
        return page;
    }

    public Page divideOrderPage(List<Order> list, int pageNo, int pageSize){
        List<Order> result=list.subList((pageNo-1)*pageSize,pageNo*pageSize);
        Page page=new Page(pageNo,pageSize,list.size());
        page.setOrderList(result);
        return page;
    }

    public Page divideCommentPage(List<Comment> list, int pageNo, int pageSize){
        List<Comment> result=list.subList((pageNo-1)*pageSize,pageNo*pageSize);
        Page page=new Page(pageNo,pageSize,list.size());
        page.setCommentList(result);
        return page;
    }

}
